package com.whli.jee.oa.controller;

import com.whli.jee.common.share.entity.ResponseBean;
import com.whli.jee.oa.entity.CustomModel;
import com.whli.jee.oa.service.ICustomModelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * <p>流程模型view</p>
 * @author whli
 * @date 2019/1/16 10:21
 */
@RestController
@RequestMapping(value = "/activity/customModel")
public class CustomModelController {

    @Autowired
    private ICustomModelService modelService;

    /**
     * 分页查询流程模型
     * @return
     * @throws Exception
     */
    @PostMapping("/listByPage")
    public ResponseBean findByPage(@RequestBody CustomModel entity) throws Exception{
        ResponseBean responseBean = new ResponseBean();
        int count = modelService.getCount(entity);
        List<CustomModel> models = modelService.listByPage(entity);
        responseBean.setCode("0");
        responseBean.setCount(count);
        responseBean.setData(models);
        return responseBean;
    }

    /**
     * 查询所有流程模型
     * @return
     * @throws Exception
     */
    @PostMapping("/listAll")
    public ResponseBean listAll() throws Exception{
        ResponseBean responseBean = new ResponseBean();
        List<CustomModel> models = modelService.listAll();
        if (models != null && models.size() > 0){
            responseBean.setCode("0");
            responseBean.setData(models);
        }
        return responseBean;
    }

    /**
     * 新建一个空模型
     */
    @PostMapping("/addModel")
    public ResponseBean addModel(@RequestBody CustomModel entity) throws IOException {
        ResponseBean responseBean = new ResponseBean();
        String modelId = modelService.addModel(entity);
        if (StringUtils.isNotEmpty(modelId)){
            responseBean.setCode("0");
            responseBean.setData(modelId);
        }
        return responseBean;
    }

    /**
     * 删除模型
     * @param entity
     * @return
     * @throws IOException
     */
    @PostMapping("/deleteModel")
    public ResponseBean deleteModel(@RequestBody CustomModel entity) throws IOException {
        ResponseBean responseBean = new ResponseBean();
        int modelId = modelService.deleteModel(entity);
        if (modelId > 0){
            responseBean.setCode("0");
            responseBean.setMessage("删除流程模型成功！");
        }
        return responseBean;
    }

    /**
     * 发布流程模型
     */
    @PostMapping("/deployModel")
    public ResponseBean deployModel(@RequestBody CustomModel entity) throws Exception {
        ResponseBean responseBean = new ResponseBean();
        int rows = modelService.deployModel(entity);
        if (rows > 0){
            responseBean.setCode("0");
            responseBean.setMessage("发布流程模型成功！");
        }
        return responseBean;
    }
}
